﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.套号管理.经销商下单;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.订单一览新.订单预览新.model;
using DXApplicationTangche.DTO;
using mendian;
using DXApplicationTangche.UC.门店下单;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using DXApplicationTangche.UC.货号管理.service;

namespace DXApplicationTangche.UC.套号管理.电商下单
{
    public partial class Form经销商选择下单 : DevExpress.XtraEditors.XtraForm
    {
        //model
        private OrderModel orderModel = new OrderModel();
        private 订单预览新Model model;
        //变量
        private String stockInventoryNo;
        private String styleNameCN;
        
        public Form经销商选择下单(OrderModel orderModel)
        {
            InitializeComponent();
            this.orderModel = orderModel;
        }
        private void Form经销商选择下单_Load(object sender, EventArgs e)
        {
            this.ActiveControl = this.textBox样衣条码;
        }
        private void textBox样衣条码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox样衣条码.Text.Trim()))
                {
                    this.stockInventoryNo = this.textBox样衣条码.Text.Trim();
                    this.selectOrderCommitDTO();
                    this.ActiveControl = this.comboBox尺码组;
                }
            }
        }
        private void selectOrderCommitDTO()
        {
            try
            {
                this.model = new 订单预览新Model(this.stockInventoryNo, Enum订单查询.按照水洗标查询订单);
            }
            catch (Exception)
            {
                MessageBox.Show("请扫描扫描成衣条码");
                return;
            }
            //清理无参考意义数据并查询款式类别
            this.model.buildStyleCategoryDTOAndClear();
            //显示该款式的尺码组
            this.comboBox尺码组.Items.Clear();
            List<String> 尺码组 = new List<string>();
            尺码组 = FunctionHelper.JsonDeserialization<List<String>>(
                        RestCall.httpGetMethod(URL.接口url + "/size/findSizeCdByStyleId?styleId=" + this.model.OrderCommitDTO返回.refStyleId));
            foreach (String str in 尺码组)
            {
                this.comboBox尺码组.Items.Add(str);
            }
        }
        
        private void comboBox尺码组_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.model.OrderCommitDTO返回.styleSizeCd = "IGS_GROUP_SIZE-" + this.comboBox尺码组.Text.Trim();
            try
            {
                this.model.尺寸 =
                    FunctionHelper.JsonDeserialization<DTOStyleMeasureItems>(
                        RestCall.httpGetMethod(
                            URL.接口url + "/style/generateStyleMeasureItems?SIZE_CD=" + this.model.OrderCommitDTO返回.styleSizeCd
                                        + "&styleID=" + this.model.OrderCommitDTO返回.refStyleId
                                        + "&categoryId=" + this.model.styleCategoryDTO.id
                                    )
                        );
                this.ActiveControl = this.numericUpDown数量;
            }
            catch
            {
                MessageBox.Show("无尺寸信息");
            }
        }
        private void richTextBox备注_TextChanged(object sender, EventArgs e)
        {
            if (this.model == null)
            {
                this.richTextBox备注.Clear();
                return;
            }
            this.model.OrderCommitDTO返回.remarks = this.richTextBox备注.Text.Trim();
        }
        private void numericUpDown数量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((NumericUpDown)sender).Text.Trim().IndexOf('.') > -1)
            {
                if (((NumericUpDown)sender).Text.Trim().Substring(((NumericUpDown)sender).Text.Trim().IndexOf('.') + 1).Length >= 0)//定义小数位
                {
                    if (!char.IsDigit(e.KeyChar))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }
                }
            }
        }

        private void simpleButton保存_Click(object sender, EventArgs e)
        {
            
            if (String.IsNullOrEmpty(this.comboBox尺码组.Text))
            {
                MessageBox.Show("选择款式尺寸");
                return;
            }
            DialogResult dialogResult = MessageBox.Show("确认保存订单吗？", "保存", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                //给orderCommitDTO参考样衣条码尺寸数量赋值
                this.model.OrderCommitDTO返回.orderNumber = Convert.ToDouble(this.numericUpDown数量.Value.ToString());
                this.model.buildOrderCommitDTOSample(this.stockInventoryNo)
                          .buildDistributorOrderCommitDTO();
                this.orderModel.buildAddSaleSampleOrderCommitDto(this.model.OrderCommitDTO返回);
                MessageBox.Show("保存成功");
                this.Close();
            }
        }

    }
}